import {
  Modal,
  message
} from 'antd'
import { genGUIDV4 } from './GuidJs'
import {
	WeaLocaleProvider,
	WeaTools
} from 'ecCom';

//打开部门页面
const openDepartment = (value) => {
  let url = '/spa/hrm/engine.html#/hrmengine/organization?'+'showTree='+false+'&type=department&id='+value;
  window.open(url);
}

//打开分部页面
const openSubcompany = (value) => {
  let url = '/spa/hrm/engine.html#/hrmengine/organization?'+'showTree='+false+'&type=subcompany&id='+value;
  window.open(url);
}

//打开人力页面
const openHrm = (value) => {
  let url = '/spa/hrm/index_mobx.html#/main/hrm/card/cardInfo/'+value;
  window.open(url);
}

//打开客户页面
const openCrm = (value) => {
  let url = '/spa/crm/mobx/index.html#/main/crm/customerView?customerId='+value;
  window.open(url);
}

//打开项目页面
const openPrj = (value) => {
  let url = '/spa/prj/index.html#/main/prj/projectCard?prjid='+value;
  window.open(url);
}

//打开新窗口
const openUrl = (redirectUrl,width = 1200,height = 600) => {
    var iTop = (window.screen.availHeight-30-height)/2;
    var iLeft = (window.screen.availWidth-10-width)/2;
    let szFeatures = "top="+iTop+"," ;
    szFeatures +="left="+iLeft+"," ;
    szFeatures +="width="+width+"," ;
    szFeatures +="height="+height+"," ;
    szFeatures +="directories=no," ;
    szFeatures +="status=yes," ;
    szFeatures +="menubar=no," ;
    szFeatures +="location=no," ;
    if (height <= 600 ) szFeatures +="scrollbars=yes," ;
    else szFeatures +="scrollbars=no," ;
    szFeatures +="resizable=yes" ; //channelmode
    window.open(redirectUrl,"_blank",szFeatures) ;
}

//打开流程的路径
const openWorkFlowPath = (workflowId,width,height) => {
  let url = '/spa/workflow/static4engine/engine.html#/main/workflowengine/path/pathSet/pathDetail/baseSet/baseInfo?';
  url = url+'workflowId='+workflowId+'&showLeftTree=false'+'&_key='+workflowId;
  openUrl(url,width,height);
}

//打开流程
const openWorkFlow = (requestid,width,height) => {
  let url = "/workflow/request/ViewRequestForwardSPA.jsp?requestid="+requestid;
  window.open(url,"_blank") ;
}

//截断最后一个逗号
const cutOff = (ids) => {
  ids = ids.substring(0,ids.lastIndexOf(","));
  return ids;
}

//获取WeaTableEdit 的拼接数据
const getTableEditData = (datas) => {
  let params = {};
  let guid1s_array = '';

  for(let item of mobx.toJS(datas)){
    let id = item.id;

    if(id == undefined){//新建的或者复制的数据是没有id的
      id = genGUIDV4();
      id = id.replace('GUID_','').replace(/_/g,'');//全局替换 '_'
    }

    guid1s_array += id + ',';
    for(let [key,value] of Object.entries(item)){
      if(!key.endsWith('span') && key != 'id'){//过滤掉span结尾和id的无效数据
        params = Object.assign(params,{
          [id+'_'+[key]]:value
        });
      }
    }
  }
    
  guid1s_array = cutOff(guid1s_array);

  params = Object.assign(params,{
                guid1s_array:guid1s_array
          });
  
  return params;
}

//一维数组去重
const doDeduplication = (arry) => {
  let temp = [];
  for(let index = 0; index < arry.length ;index++){
      if( !temp.includes(arry[index])){
        temp.push(arry[index]);
      }
  }

  return temp;
}

//比较两个日期的大小  YYYY-MM-DD
const doCompareDate = (startDate,endDate) => {
  let startDateTemp = new Date(startDate);
  let endDateTemp = new Date(endDate);

  return startDateTemp > endDateTemp;
}

//比较两个日期是否相等  YYYY-MM-DD
const isEqualDate = (startDate,endDate) => {
  let startDateTemp = new Date(startDate);
  let endDateTemp = new Date(endDate);

  return startDateTemp === endDateTemp;
}

//是否为 "" 或者 undifined
const isUndifinedOrNull = (value) => {
  if(value === undefined || value === ""){
    return true;
  }
  return false;
}

//获取模块图标 如'finance'
const getIcon = (value) => {
	return WeaTools.getIconBGC(value);
}

//设置多语言传参
const setMulitLang = (value) => {
  return  Object.assign(value,{
              is_multilang_set:true
          }); 
}

//将十六进制的颜色转换成rgb
const hex2Rgb = (hex) => { //十六进制转为RGB
  let rgb = []; // 定义rgb数组
  if (/^\#[0-9A-F]{3}$/i.test(hex)) { //判断传入是否为#三位十六进制数
   let sixHex = '#';
   hex.replace(/[0-9A-F]/ig, function(kw) {
    sixHex += kw + kw; //把三位16进制数转化为六位
   });
   hex = sixHex; //保存回hex
  }
  if (/^#[0-9A-F]{6}$/i.test(hex)) { //判断传入是否为#六位十六进制数
   hex.replace(/[0-9A-F]{2}/ig, function(kw) {
    rgb.push(eval('0x' + kw)); //十六进制转化为十进制并存如数组
   });
   return `${rgb.join(',')}`; //输出RGB格式颜色
  } else {
   return '0,0,0';
  }
 }

 //将一层key的对象拼接成url的参数string 
 const obj2UrlParam = (obj) =>{
  let queryParams = '';

  for(let key of Object.keys(obj)){
    let item = key + '=' + obj[key];
    if(queryParams != ''){
      queryParams += "&"+item;
    } else {
      queryParams += item;
    }
  }

  return queryParams;
 }

const fnaIconClass = {
   'save':'icon-coms-Preservation',
   'add':'icon-coms-Add-to-o',
   'batchDelete':'icon-coms-Batch-delete',
   'test':'icon-coms-operation',
   'copy':'icon-coms-form-copy',
   'dowload':'icon-coms-leading-in',
   'export':'icon-coms-export',
   'log':'icon-coms-Print-log',
   'cancel':'icon-coms-go-back'
   };

//获取右键菜单图标Class
const getFnaIconClass = (type) => {
  return fnaIconClass[type] ? fnaIconClass[type] : '';
}

export {
  openUrl,
  openWorkFlowPath,
  openWorkFlow,
  cutOff,
  getTableEditData,
  doDeduplication,
  doCompareDate,
  getIcon,
  setMulitLang,
  isEqualDate,
  isUndifinedOrNull,
  hex2Rgb,
  getFnaIconClass,
  obj2UrlParam
}